﻿Public Class ProjectImageControl

#Region "构造函数"

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Sub New(ByVal id As Integer, ByVal imageName As String, ByVal path As String, ByVal pi As ProjectItem)
        MyClass.New()

        _imageOrder = id
        _imagePath = path
        _ImageName = imageName

        _pi = pi
    End Sub


    Public Sub New(ByVal pii As ProjectImageItem, ByVal pi As ProjectItem)
        MyClass.New()

        _projectID = pii.projectID
        _imageOrder = pii.ImageOrder
        _imagePath = pii.imagePath
        _ImageName = pii.imageName

        _pii = pii

        _pi = pi
    End Sub

#End Region

    Private _pi As ProjectItem = Nothing

    Private _pii As ProjectImageItem


    Private Sub ProjectImageControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Init()
        Me.btnDelete.Image = Nothing
        Me.btnConfig.Image = Nothing
    End Sub

    ''' <summary>
    ''' 初始化，显示图片
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Init()
        refreshControl()
    End Sub

    Public Sub refreshControl()
        If _imagePath = "" Then
            Me.lblLayName.Text = ""
        Else
            Me.lblLayName.Text = _ImageName
            If System.IO.File.Exists(_imagePath) Then
                Try
                    Me.PictureBox1.Image = Image.FromFile(ImagePath)
                Catch ex As Exception
                    msg.ShowError("打开图片时发生错误。", ex)
                End Try
            End If
        End If
    End Sub


    Private _imagePath As String
    Public Property ImagePath() As String
        Get
            Return _imagePath
        End Get
        Set(ByVal value As String)
            _imagePath = value
        End Set
    End Property


    Private _imageOrder As Integer
    Public Property ImageOrder() As Integer
        Get
            Return _imageOrder
        End Get
        Set(ByVal value As Integer)
            _imageOrder = value
        End Set
    End Property


    Private _ImageName As String
    Public Property ImageName() As String
        Get
            Return _ImageName
        End Get
        Set(ByVal value As String)
            _ImageName = value
            Me.lblLayName.Text = _ImageName
        End Set
    End Property


    Private _projectID As String
    Public Property projectID() As String
        Get
            Return _projectID
        End Get
        Set(ByVal value As String)
            _projectID = value
        End Set
    End Property

    Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
        ''Dim frm As New LargeImageViewForm()
        ''frm.SetImage(_imagePath)
        ''frm.ShowDialog()

        'Dim frm As New ProjectImageAddForm(_pii)
        'If frm.ShowDialog() = DialogResult.OK Then            
        '    Me._ImageName = _pii.imageName
        '    Me._imagePath = _pii.imagePath
        '    Me._imageOrder = _pii.ImageOrder
        '    RaiseEvent ImageModifyEvent(Me, New EventArgs())
        'End If
    End Sub

    Public Event ImageModifyEvent As EventHandler
    Public Event ImageDeleteEvent As EventHandler

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim tip As String
        tip = String.Format("是否要删除【{0}】图层？", Me._ImageName)
        If msg.ShowYesNo(tip) = DialogResult.Yes Then
            If _pii IsNot Nothing Then
                ProjectImageService.Delete(_pii)
                'Log.log(tbccLogOperateType.ModifyProject, String.Format("删除图层({0})", _pii.imageName))
            End If

            RaiseEvent ImageDeleteEvent(Me, New EventArgs())
        End If
    End Sub

    Private Sub btnConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfig.Click
        Dim frm As New ImageControlConfigForm(_pii, _pi)
        If frm.ShowDialog() = DialogResult.OK Then
            RaiseEvent ImageModifyEvent(Me, New EventArgs())
        End If
    End Sub

    Private Sub btnbmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbmp.Click
        Dim frm As New ProjectImageAddForm(_pii)
        If frm.ShowDialog() = DialogResult.OK Then
            Me._ImageName = _pii.imageName
            Me._imagePath = _pii.imagePath
            Me._imageOrder = _pii.ImageOrder
            RaiseEvent ImageModifyEvent(Me, New EventArgs())
        End If
    End Sub
End Class
